package javaapplication1;

/**
 *
 * @author xerxes
 */
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.lang.*;

public class Main {

    BufferedReader reader;
    BigInteger rem;
    BigInteger prev;
    int N, B;

    public static void main(String[] args) throws IOException {
        Main ob = new Main();
        ob.run();
        System.exit(0);
    }

    public void run() throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        //reader = new BufferedReader(new FileReader("in.txt"));
        String line = "";
        while ((line = reader.readLine().trim()) != null) {
            StringTokenizer st = new StringTokenizer(line);


            B = Integer.parseInt(st.nextToken());
            N = Integer.parseInt(st.nextToken());
            if (B == 0 && N == 0) {
                break;
            }
            doRest();
        }
    }

    public void doRest() {
        go(1);
        System.out.println(prev);
    }

    public void go(int nwlen) {
        if (nwlen > N) {
            return;
        } else if (nwlen == 1) {
            prev = BigInteger.valueOf((long) B - 1);
            rem = BigInteger.ONE;
            go(nwlen + 1);
        } else {
            BigInteger temp = prev.multiply(BigInteger.valueOf((long) B));
            temp = temp.subtract(rem);
            rem = prev;
            prev = temp;
            go(nwlen + 1);
        }
    }

    public int nextInt(String e) {
        return Integer.parseInt(e);
    }
}
